<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>刷卡记录</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="hr.css" />
</head>
<body>

<?php require 'hrfuns.php';

function getAllId($db)
{
    $allid = array();
    $results = $db->query("select distinct card_id from login_t order by card_id");
    while ($row = $results->fetchArray()) {
        $allid[] = $row["card_id"];
    }
    return $allid;
}

function getAllDate($db, $month)
{
    $alldate = array();
    $sqlstr = "select distinct substr(login_time, 1, 8) v_date from login_t"
           . " where substr(login_time, 1, 6) = '$month'";
//echo $sqlstr;
    $results = $db->query($sqlstr);

    while ($row = $results->fetchArray()) {
        $alldate[] = $row["v_date"];
    }
    return $alldate;

}

function getTime($db, $v_id, $v_date)
{
    $dates = array();
    $sqlstr = "select substr(login_time, 9) v_time from login_t" 
            . " where card_id='$v_id' and substr(login_time, 1, 8) = '$v_date'";
    $results = $db->query($sqlstr);
                        
    while ($row = $results->fetchArray()) {
        $time = $row["v_time"];
        $dates[] = substr($time, 0, 2) . ":" . substr($time, 2, 2);    
    }
    if (count($dates) == 0)
        return "&nbsp;";
    else
        return join("  ", $dates);    

}

if (isset($_POST["submit"])) {
    $month = $_POST['month'];

    $start_time = time();
    
    $db = GetDB();
    $allid = getAllId($db);
    $alldate = getAllDate($db, $month);
    echo "<table bgcolor=#6f6f6f cellspacing=1 cellpadding=1 border=0>\n";
    echo "<tr>\n";
    echo "    <th>卡号</th><th>". join("</th><th>", $alldate) . "</th>\n";

    echo "</tr>\n";
    foreach ($allid as $v_id) {
        echo "<tr>\n";
        echo "    <td>$v_id</td>";
        foreach ($alldate as $v_date) {
            echo "<td>" . getTime($db, $v_id, $v_date) . "</td>";
        }
        echo "</tr>\n";
    }

    echo "<table>\n";

    $end_time = time();

    $db->close();  
}
?>
  
</body>
</html>

